package com.csx.saas.platform.system.controller;

import com.csx.basic.annotation.log.SysLog;
import com.csx.basic.base.R;
import com.csx.basic.base.controller.SuperController;
import com.csx.basic.database.mybatis.conditions.Wraps;
import com.csx.saas.platform.system.dto.SaveRoleResourceDTO;
import com.csx.saas.platform.system.dto.SystemRoleAuthorityPageQuery;
import com.csx.saas.platform.system.dto.SystemRoleAuthoritySaveDTO;
import com.csx.saas.platform.system.dto.SystemRoleAuthorityUpdateDTO;
import com.csx.saas.platform.system.entity.SystemRoleAuthority;
import com.csx.saas.platform.system.service.SystemRoleAuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 角色的资源
 * </p>
 *
 * @author ChenSongXin
 * @date 2021-02-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/systemRoleAuthority")
@Api(value = "SystemRoleAuthority", tags = "角色的资源")
//@PreAuth(replace = "platform:systemRoleAuthority:")
public class SystemRoleAuthorityController extends SuperController<SystemRoleAuthorityService, Long, SystemRoleAuthority, SystemRoleAuthorityPageQuery, SystemRoleAuthoritySaveDTO, SystemRoleAuthorityUpdateDTO> {

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<SystemRoleAuthority> systemRoleAuthorityList = list.stream().map((map) -> {
            SystemRoleAuthority systemRoleAuthority = SystemRoleAuthority.builder().build();
            //TODO 请在这里完成转换
            return systemRoleAuthority;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(systemRoleAuthorityList));
    }

    @ApiOperation(value = "查询的授权角色所有资源id", notes = "查询的授权角色所有资源id")
    @GetMapping("/resource/{roleId}")
    @SysLog("查询的授权角色所有资源id")
    public R<List<Object>> resource(@PathVariable("roleId") Long roleId) {
        List<Object> resourceIds = baseService.getBaseMapper().selectObjs(Wraps.<SystemRoleAuthority>lbQ().eq(SystemRoleAuthority::getRoleId, roleId).select(SystemRoleAuthority::getAuthorityId));
        return R.success(resourceIds);
    }

    @ApiOperation(value = "保存授权角色资源id", notes = "保存授权角色资源id")
    @PostMapping("/save/resource")
    @SysLog("保存授权角色资源id")
    public R<Boolean> saveResource(@RequestBody SaveRoleResourceDTO saveRoleResourceDTO) {
        baseService.saveResource(saveRoleResourceDTO);
        return R.success();
    }
}
